package com.tuanzi.loan.workflow.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.workflow.entity.StartPage;
import com.tuanzi.loan.workflow.repository.StartPageRepository;
import com.tuanzi.loan.workflow.service.StartPageService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Ray
 * @date 2018/01/16
 */
@Slf4j
@Service
@Transactional
public class StartPageServiceImpl implements StartPageService {

	@Autowired
	private StartPageRepository startPageRepository;

	@Override
	public StartPage saveOrUpdate(StartPage startPage) {
		return startPageRepository.save(startPage);
	}

	@Override
	public StartPage findByProcDefId(String procDefId) {
		return startPageRepository.findByProcDefId(procDefId);
	}

	@Override
	public void deleteByProcDefId(String procDefId) {
		StartPage startPage = findByProcDefId(procDefId);
		if (startPage != null) {
			startPageRepository.delete(startPage);
		}
	}

	@Override
	public void synchronizeStartPage(String oldProcDefId, String newProcDefId) {
		StartPage oldStartPage = findByProcDefId(oldProcDefId);

		if (oldStartPage != null) {
			StartPage newStartPage = new StartPage();
			BeanUtils.copyProperties(oldStartPage, newStartPage, new String[] { "id" });
			newStartPage.setProcDefId(newProcDefId);
			startPageRepository.save(newStartPage);
		}
	}
}